
package eft.android.view;

import me.croute.calendarexample.R;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import eft.android.controller.Controller;


public class AddWorkout extends Activity{

	private Long date;
	private long ID;
	private Controller controller;
	private View view;
	/*
	private String title;
	private String descrip;
	private String weight;
	private String reps;
	private long time;
	private String notes;
	 */

	@Override
	public void onCreate(Bundle savedInstanceState) {
		try {
			super.onCreate(savedInstanceState);
			setContentView(R.layout.addworkout);

			Bundle extras = getIntent().getExtras();

			if( extras != null ){
				if ( extras.getString("Title") != null ){
					setwoTitle ( extras.getString("Title") );
				}

				if ( extras.getString("Description") != null ){
					setDescrip( extras.getString("Description") );
				}

				if ( extras.getString("Weight") != null ){
					setWeight ( extras.getString("Weight") );
				}

				if ( extras.getString("Reps") != null ){
					setReps ( extras.getString("Reps") );
				}

				if ( extras.getLong("Time") != 0 ){
					long time = extras.getLong("Time");

					setTimeMins( time/6000 );
					setTimeSecs( time%6000 );
				}

				if ( extras.getString("Notes") != null ){
					setNotes( extras.getString("Notes") );
				}

				if( extras.getLong("Date") != 0 ){
					setDate( extras.getLong("Date") );
				}

				if( extras.getLong("ID") != 0 ){
					setID( extras.getLong("ID") );
					Button buttonAddWorkout = (Button) findViewById(R.id.addWorkoutAddWorkout);
					buttonAddWorkout.setText("Update Workout");
				}


			}

			controller = Controller.getInstance();

			initVars();
		}
		catch (Exception e) {
			Log.e("ERROR", "ERROR IN CODE: " + e.toString());
			e.printStackTrace();
		}
	}


	private void initVars() {

		final Button cfWorkout = (Button) findViewById(R.id.addWorkoutcfWorkout);

		cfWorkout.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {

				Intent populateFromDB = new Intent(getApplicationContext(), PopulateFromDB.class);
				populateFromDB.putExtra(  "Date", getDate());
				startActivity(populateFromDB);
			}
		});

		final Button buttonAddWorkout = (Button) findViewById(R.id.addWorkoutAddWorkout);
		buttonAddWorkout.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if( getwoTitle() == null || getwoTitle() == "" ){

					Context context = getApplicationContext();
					CharSequence text = "Title field may not be empty.\nPlease enter a title.";
					int duration = Toast.LENGTH_LONG;

					Toast toast = Toast.makeText(context, text, duration);
					toast.show();

				}
				else{
					if( ID != 0 ){
						controller.updateWorkout(getID(), getwoTitle(), getDescrip(), getDate(), getReps(), getWeight(), getTimeMinutes(), getTimeSeconds(), getNotes() );

						Context context = getApplicationContext();
						CharSequence text = "Workout Updated";
						int duration = Toast.LENGTH_LONG;

						Toast toast = Toast.makeText(context, text, duration);
						toast.show();

						Intent backToCalendar = new Intent(getApplicationContext(), CalendarViewActivity.class);
						startActivity(backToCalendar);
					}
					else{
						controller.addWorkout(getwoTitle(), getDescrip(), getDate(), getReps(), getWeight(), getTimeMinutes(), getTimeSeconds(), getNotes() );

						Context context = getApplicationContext();
						CharSequence text = "Workout Added";
						int duration = Toast.LENGTH_LONG;

						Toast toast = Toast.makeText(context, text, duration);
						toast.show();


						Intent backToCalendar = new Intent(getApplicationContext(), CalendarViewActivity.class);
						startActivity(backToCalendar);
					}
				}

			}
		});	
	}

	public boolean setwoTitle( String s )
	{
		if( s == null ) return false;
		TextView field = (TextView) findViewById(R.id.addWorkoutTitle);
		field.setText(s);
		return true;	
	}

	public String getwoTitle()
	{
		TextView txt = (TextView) findViewById(R.id.addWorkoutTitle);
		return txt.getText().toString().trim();
	}

	public boolean setDescrip( String s )
	{
		if( s == null ) return false;
		TextView field = (TextView) findViewById(R.id.addWorkoutDescription);
		field.setText(s);
		return true;
	}

	public String getDescrip()
	{
		TextView txt = (TextView) findViewById(R.id.addWorkoutDescription);
		return txt.getText().toString();
	}

	public boolean setRwt( String r, String w, String t )
	{
		boolean allSuccess = true;

		TextView repsField = (TextView) findViewById(R.id.addWorkoutReps);

		TextView weightsField = (TextView) findViewById(R.id.addWorkoutWeight);

		TextView timeField = (TextView) findViewById(R.id.addWorkoutTime);

		if( r == null ) { allSuccess = false; }
		else{ repsField.setText(r); }

		if( w == null ) { allSuccess = false; }
		else{ weightsField.setText(w);}

		if( t == null ) { allSuccess = false; }
		else{ timeField.setText(t); }

		return allSuccess;
	}

	public void setReps(String s)
	{
		TextView txt = (TextView) findViewById(R.id.addWorkoutReps);
		txt.setText(s.trim());
	}

	public void setWeight(String s)
	{
		TextView txt = (TextView) findViewById(R.id.addWorkoutWeight);
		txt.setText(s.trim());
	}


	public void setTimeMins(long d){
		TextView txt = (TextView) findViewById(R.id.addWorkoutTime);
		txt.setText( ((Long) d ).toString() );
	}

	public void setTimeSecs(double d){
		TextView txt = (TextView) findViewById(R.id.addWorkoutTime2);
		txt.setText( ((Double) d ).toString() );
	}

	public String getReps()
	{
		TextView txt = (TextView) findViewById(R.id.addWorkoutReps);
		return txt.getText().toString();
	}

	public String getWeight()
	{
		TextView txt = (TextView) findViewById(R.id.addWorkoutWeight);
		return txt.getText().toString();
	}


	public String getTimeMinutes()
	{
		TextView txt = (TextView) findViewById(R.id.addWorkoutTime);


		return txt.getText().toString();
	}

	public String getTimeSeconds()
	{
		TextView txt = (TextView) findViewById(R.id.addWorkoutTime2);

		return txt.getText().toString();
	}

	/*
	public long getFormattedTime()
	{
		TextView txtmins = (TextView) findViewById(R.id.addWorkoutTime);
		TextView txtsecs = (TextView) findViewById(R.id.addWorkoutTime2);

		long mins = Long.parseLong( txtmins.getText().toString() );
		double secs = Double.parseDouble( txtsecs.getText().toString() );
		long secs2 = (long) secs * 100;

		return mins*60 + secs2;
	}
	 */


	public boolean setNotes( String s )
	{
		if( s == null ) return false;
		TextView field = (TextView) findViewById(R.id.addWorkoutNotes);
		field.setText(s);
		return true;

	}

	public String getNotes()
	{
		TextView txt = (TextView) findViewById(R.id.addWorkoutNotes);
		return txt.getText().toString();
	}

	public void setDate( long d ){
		date = d;
	}

	public long getDate(){
		return date;
	}




	@Override
	public void onBackPressed() {        
		Intent backToCalendar = new Intent(getApplicationContext(), CalendarViewActivity.class);
		startActivity(backToCalendar);
	}


	public void setID(long iD) {
		ID = iD;
	}


	public long getID() {
		return ID;
	}


}


